<template>
  <div class="contract-list">
    <top-header></top-header>

    <div class="search-div">
      <div class="search-one-div">
        <search-one @search="onSearch"></search-one>
      </div>

      <span class="iconfont iconshaixuan"></span>
    </div>

    <div class="common-list-body" v-fullheight>
      <empty-one v-show="!listSource || listSource.length === 0"></empty-one>
      <a href="javascript:;" @click="changeAttachmentsShow(item.bmsAttachments)" class="list-item" v-for="(item, key) in listSource" :key="key">
        <div class="name">{{ item.name }}</div>
        <div class="status code">
          <span>合同编号：</span>
          <span>{{ item.code }}</span>
        </div>
        <div class="status date">
          <span>签约日期：</span>
          <span>{{ item.signDate }}</span>
        </div>
        <div class="status">
          <span class="status-span">{{ item.nature }}</span>
        </div>
      </a>
    </div>
    <van-popup v-model="attachmentsShow" position="bottom" round>
      <div class="attachments">
        <a :href="$filePreview(item.fileName, item.storagePath)" v-for="(item, key) in attachments" :key="key">
          <span class="iconfont" :class="$tools.getFileIcon(item.storagePath)"></span>
          <span class="fine-name">{{ item.fileName }}</span>
        </a>
      </div>
    </van-popup>
  </div>
</template>

<script lang="ts">
import { Component } from "vue-property-decorator";
import TopHeader from "@/components/TopHeader.vue";
import { Popup } from "vant";
import SearchOne from "@/components/SearchOne.vue";
import EmptyOne from "@/components/EmptyOne.vue";
import Api from "@/api";
import Base from "@/utils/base";

@Component({
  name: "ContractList",
  components: {
    TopHeader,
    SearchOne,
    EmptyOne,
    VanPopup: Popup
  }
})
export default class ContractList extends Base {
  private listSource: any[] = [];
  private loading = false;
  private attachmentsShow = false;
  private attachments: any[] = [];

  private apiParams = {
    projectId: "",
    searchValue: ""
  };

  private onSearch() {
    this.$cPromise.all([this.getData()]);
  }

  private created() {
    this.apiParams.projectId = (this.$route.query.projectId ? this.$route.query.projectId : "") as string;
    if (this.apiParams.projectId) {
      this.$cPromise.all([this.getData()]);
    }
  }

  private changeAttachmentsShow(item: any) {
    if (!item || item.length === 0) {
      this.$toast({
        message: "您没有附件可供预览",
        forbidClick: true
      });
      return;
    }
    this.attachmentsShow = true;
    this.attachments = item;
  }

  private async getData() {
    const res = await Api.GetProjectContractList(this.listParams);
    if (res && res.code === 200) {
      this.listSource = res.rows;
    }
    return res;
  }

  /**
   * 预设变量，可能后期扩展可以用到
   */
  get listParams() {
    const _return = this.apiParams;
    return _return;
  }
}
</script>

<style lang="less" scoped>
.contract-list {
  .search-div {
    background-color: white;
    display: flex;
    align-items: center;
    .iconshaixuan {
      padding: 10px 10px 10px 0px;
      font-size: 22px;
      color: #bfbfbf;
    }
    .search-one-div {
      flex: 1;
    }
  }

  .list-item {
    margin: 10px 10px 0px 10px;
    background: #ffffff;
    border-radius: 8px;
    // height: 125px;
    display: block;
    padding: 15px;
    position: relative;
    &::before {
      top: 18px;
      left: 0px;
      content: "";
      position: absolute;
      width: 3px;
      height: 14px;
      background: linear-gradient(281deg, #5a96fa -338%, #6dc1fd 93%);
    }
    .name {
      font-size: 16px;
      // overflow: hidden;
      // white-space: nowrap;
      // text-overflow: ellipsis;
      // word-break: break-all;
    }
    .status {
      margin-top: 10px;
      display: flex;
      align-content: center;
      color: #717787;
    }
    .status-span {
      padding: 0 6px;
      height: 18px;
      line-height: 18px;
      text-align: center;
      font-size: 12px;
      color: #65b0fb;
      border-radius: 18px;
      border: 1px solid #65b0fb;
    }
    .date {
      color: #a1a6b3;
    }
  }
  .attachments {
    max-height: 400px;
    overflow: auto;
    padding: 16px;

    a {
      position: relative;
      display: flex;
      padding: 12px 0px;
      border-bottom: 1px dotted #dddddd;
      align-items: center;
      color: #444444;

      &:last-child {
        border-bottom: 0;
      }

      &:active {
        opacity: 0.5;
      }

      .iconfont {
        line-height: 130%;
      }

      .fine-name {
        margin-left: 16px;
      }
    }
  }
}
</style>
